package org.example.websocket.raw.client;


import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.net.URI;

@Slf4j
@Component
public class MyWebSocketClient extends WebSocketClient {

    @EventListener(ContextRefreshedEvent.class)
    public void test() throws InterruptedException {
        this.connectBlocking();
        this.send("测试数据[raw]！！！");
    }

    public MyWebSocketClient() {
        super(URI.create("ws://localhost:9001/raw/ws"));
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        log.info("连接已建立！！！");
    }

    @Override
    public void onMessage(String message) {
        log.info("收到服务端发来的消息：" + message);
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        log.info("连接已关闭！！！");
    }

    @Override
    public void onError(Exception ex) {
        log.info("发生异常：", ex);
    }
}
